/**
 * 项目：ystree
 * 模块：树工厂
 * 作者：yangjianliang
 * 日期：2007-7-2
 */
package net.ysen.tree;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TreeFactory {

	private static final Log log = LogFactory.getLog(TreeFactory.class);
	
	private static final Class DefaultTreeClass = net.ysen.tree.impl.YstreeImpl.class;

	/**
	 * 返回ystree自带的树操作类。该类默认实现了生成html和 xml 的树结构代码
	 * @return
	 */
	public static final ITree getInstance(){
		ITree tree = null;
		try {
			tree = (ITree) DefaultTreeClass.newInstance() ;
		} catch (InstantiationException e) {
			log.error(e);
			
		} catch (IllegalAccessException e) {
			log.error(e);
		}
		return tree ;
	}
	
	/**
	 * 返回一个树对象实例
	 * @param treeClassStr 使用哪个树对象操作类
	 * @param useDefaultClassIfException 是否在发生异常时使用默认的树操作类替换指定的树操作类
	 * @return
	 */
	public static final ITree getInstance( final String treeClassStr, boolean useDefaultClassIfException){
		ITree tree = null;
		try {
			Class treeimpl = Class.forName( treeClassStr );
			tree = (ITree) treeimpl.newInstance() ;
		} catch (Exception e) {
			log.error(e);
			if( useDefaultClassIfException ){
				tree = getInstance();
			}
		}
		return tree ;
	}
}
